package com.SmartLED.Common;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一异常处理类
 * Created by Frozen on 2017/1/9.
 */

@ControllerAdvice
public class BaseException{

    private static Logger logger = Logger.getLogger("exception");
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Map jsonErrorHandle(HttpServletRequest request, Exception e) throws Exception{
        StackTraceElement [] traceElement = e.getStackTrace();
        //记录此次异常出现的时间
        logger.error("--------------" + new Date() + "--------------");
        //输出造成异常的原因
        logger.error(e.getCause());
        //输出异常堆栈信息
        for(int i = 0; i < traceElement.length ; i++)
        {
            logger.error(traceElement[i]);
        }
        Map map = new HashMap();
        map.put("errorMsg","未知错误");
        map.put("code","1");
        return map;
    }
}
